package net.roseparadise.dao.hibernate;

import java.math.BigInteger;
import java.sql.SQLException;
import java.util.List;

import net.roseparadise.dao.AlbumDAO;
import net.roseparadise.model.Album;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class AlbumDAOImpl  extends HibernateDaoSupport implements AlbumDAO{
    private static final Log log = LogFactory.getLog(AlbumDAOImpl.class);
	//property constants
	
	public static final String ISSTUDIO = "isstudio";
	public static final String MEMBERID = "memberid";
	public static final String CREATEDATE = "createdate";
	public static final String ALBUMNAME = "albumname";




	protected void initDao() {
		//do nothing
	}
    
    public void save(Album transientInstance) {
        log.debug("saving Album instance");
        try {
            getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(Album persistentInstance) {
        log.debug("deleting Album instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public Album findById( java.lang.Integer id) {
        log.debug("getting Album instance with id: " + id);
        try {
            Album instance = (Album) getHibernateTemplate()
                    .get(Album.class, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    public List getStudioAlbumList(final int startRow, final int maxCount) {   
        final String sql = "FROM Album A WHERE A.isstudio='Y' ORDER BY A.albumorder ASC , A.albumid ASC" ;   
    	    return getHibernateTemplate().executeFind(new HibernateCallback() {   
    	    	public Object doInHibernate(Session session)   
    	                throws HibernateException, SQLException {   
    	            return session.createQuery(sql).setFirstResult(startRow)   
    	                    .setMaxResults(maxCount).list();   
    	        }   
    	    });   
    	}  
    public BigInteger countStudioAlbumList() {   
        final String sql = "SELECT COUNT(*) FROM TALBUM A WHERE A.ISSTUDIO='Y'" ;   
    	    return (BigInteger)getHibernateTemplate().execute(new HibernateCallback() {   
    	    	public Object doInHibernate(Session session)   
    	                throws HibernateException, SQLException {   
    	    		return (BigInteger)(session.createSQLQuery(sql).uniqueResult());   
    	        }   
    	    });   
    	}  
    public List getMemberAlbumList(final int memberid,final int startRow, final int maxCount) {   
        final String sql = "FROM Album A WHERE A.isstudio='N' AND A.memberid=? ORDER BY A.albumorder ASC , A.albumid ASC" ;   
    	    return getHibernateTemplate().executeFind(new HibernateCallback() {   
    	    	public Object doInHibernate(Session session)   
    	                throws HibernateException, SQLException {   
    	            return session.createQuery(sql).setInteger(0, memberid).setFirstResult(startRow)   
    	                    .setMaxResults(maxCount).list();   
    	        }   
    	    });   
    	}  
    public BigInteger countMemberAlbumList(final int memberid) {   
        final String sql = "SELECT COUNT(*) FROM TALBUM A WHERE A.ISSTUDIO='N' AND A.MEMBERID=?" ;   
    	    return (BigInteger)getHibernateTemplate().execute(new HibernateCallback() {   
    	    	public Object doInHibernate(Session session)   
    	                throws HibernateException, SQLException {   
    	            return (BigInteger)(session.createSQLQuery(sql).setInteger(0, memberid).uniqueResult());   
    	        }   
    	    });   
    	}  
    
    public List<Album> findByExample(Album instance) {
        log.debug("finding Album instance by example");
        try {
            List<Album> results = (List<Album>) getHibernateTemplate().findByExample(instance); 
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding Album instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from Album as model where model." 
         						+ propertyName + "= ?";
		 return getHibernateTemplate().find(queryString, value);
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}
	
	public List<Album> findByIsstudio(Object isstudio
	) {
		return findByProperty(ISSTUDIO, isstudio
		);
	}
	
	public List<Album> findByMemberid(Object memberid
	) {
		return findByProperty(MEMBERID, memberid
		);
	}
	
	public List<Album> findByCreatedate(Object createdate
	) {
		return findByProperty(CREATEDATE, createdate
		);
	}
	
	public List<Album> findByAlbumname(Object albumname
	) {
		return findByProperty(ALBUMNAME, albumname
		);
	}
	

	

	public List findAll() {
		log.debug("finding all Album instances");
		try {
			String queryString = "from Album";
		 	return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public Album merge(Album detachedInstance) {
        log.debug("merging Album instance");
        try {
            Album result = (Album) getHibernateTemplate()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }



	public static AlbumDAOImpl getFromApplicationContext(ApplicationContext ctx) {
    	return (AlbumDAOImpl) ctx.getBean("ALBUMDAO");
	}

}
